package com.maxsalad.common.message;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;

import java.util.Locale;

/**
 * Created by 2014-400 on 2016-03-07.
 */
public class SpringMessageResourceMessages implements IMessage, ApplicationListener<ApplicationEvent> {
    protected ApplicationContext context = null;

    @Override
    public String getKVMessage(String code) {
        assert (context != null);
        return code + ":" + context.getMessage(code, null, Locale.SIMPLIFIED_CHINESE);
    }

    @Override
    public String getKVMessage(String code, Object... args) {
        assert (context != null);
        return code + ":" + context.getMessage(code, args, Locale.SIMPLIFIED_CHINESE);
    }

    @Override
    public String getMessage(String code) {
        assert (context != null);
        return context.getMessage(code, null, Locale.SIMPLIFIED_CHINESE);
    }

    @Override
    public String getMessage(String code, Object... args) {
        assert (context != null);
        return context.getMessage(code, args, Locale.getDefault());
    }

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ContextRefreshedEvent) {
            context = ((ContextRefreshedEvent) event).getApplicationContext();
            return;
        }
        if (event instanceof ContextClosedEvent) {
            context = null;
        }
    }
}
